import { Module } from 'vuex'
import { ILoginState } from './types'
import { IRootState } from '../types'
import {
  loginRequest,
  getCodeKeyRequest,
  phoneCodeRequest,
  phoneCodeLoginRequest,
  getCodeImgRequest,
  getCodeStatusRequest
} from '@/service/login/login'
import { IAccount, IPhone } from '@/service/login/types'
import localCache from '@/utils/cache'
import router from '../../router/index'

const loginModule: Module<ILoginState, IRootState> = {
  namespaced: true,
  state() {
    return {
      cookie: '',
      token: '',
      userInfo: {
        id: 0,
        userName: '',
        avatarUrl: '',
        nickname: '',
        birthday: 0,
        signature: '',
        city: 0,
        followeds: 0,
        follows: 0,
        vipType: 0
      },
      codeImg: '',
      codeKey: '',
      codeMessage: 0
    }
  },
  mutations: {
    changeLoginState(state, loginData) {
      localCache.setCache('token', loginData.token)

      state.token = loginData.token
      state.cookie = loginData.cookie
      state.userInfo.avatarUrl = loginData.profile.avatarUrl
      state.userInfo.birthday = loginData.profile.birthday
      state.userInfo.id = loginData.account.id
      state.userInfo.nickname = loginData.profile.nickname
      state.userInfo.signature = loginData.profile.signature
      state.userInfo.userName = loginData.account.userName
      state.userInfo.city = loginData.profile.city
      state.userInfo.followeds = loginData.profile.followeds
      state.userInfo.follows = loginData.profile.follows
      state.userInfo.vipType = loginData.profile.vipType
      localCache.setCache('ID', loginData.account.id)
      // router.beforeEach((to, from, next) => {
      //   if (to.path === '/main') {

      //   }
      // })
      router.push('/main')
      const routePath = window.location.href.split('#').pop()
      if (routePath !== '/login') {
        router.push(`${routePath}`)
        console.log(window.location.href.split('#').pop(), 'router')
      }
      // console.log(location.hash, 99999)
    },
    changeCodeState(state, codeImgRes) {
      state.codeImg = codeImgRes.qrimg
    },
    changeCodeStatus(state, codeStatusRes) {
      state.codeMessage = codeStatusRes.message
    }
  },
  actions: {
    async loginAction({ commit }, payload: IAccount) {
      const loginResult = await loginRequest(payload)
      commit('changeLoginState', loginResult)
    },
    async getPhoneCodeAction({ commit }, phone: IPhone) {
      await phoneCodeRequest(phone)
    },
    async phoneLoginAction({ commit }, phone: IPhone) {
      const phoneRes = await phoneCodeLoginRequest(phone)
      commit('changeLoginState', phoneRes)
    },
    async getCodeAction({ commit }) {
      const codeRes = await getCodeKeyRequest()
      const codeKey = codeRes?.data.unikey
      const codeImgRes = await getCodeImgRequest(codeKey)
      commit('changeCodeState', codeImgRes.data)
    },
    async getCodeStatusAction({ commit }, codeKey: string) {
      const codeStatusRes = await getCodeStatusRequest(codeKey)
      commit('changeCodeStatus', codeStatusRes)
    }
    // loadLocalLogin({ commit, dispatch }) {
    //   const token = localCache.getCache('token')
    //   const password = localCache.getCache('password')
    //   const userName = localCache.getCache('userName')
    //   const userFrom = {
    //     userPassword: password,
    //     userName: userName
    //   }
    //   if (token) {
    //     dispatch('loginAction', { ...userFrom })
    //     console.log(5554545225)
    //   }
    // }
  },
  getters: {}
}

export default loginModule
